module.exports = class UiKit {
	static loadingNumer = 0;
	static loadingTimer;
	static copy(data) {
		uni.setClipboardData({
			data: data,
			success: function (data) {
				uni.showToast({
					title: "复制成功",
					duration: 2000,
				});
			},
			fail: function (err) {},
			complete: function (res) {},
		});
	}
	static navTo(url) {
		uni.navigateTo({
			url: url,
		});
	}
	static valueChange(e, that) {
		if (e.target.id) {
			that[e.target.id] = e.detail.value;
		} else {
			let targetId = e.target.dataset.id;
			let ids = targetId.split(".");
			let tempObject = that;
			for (let nIndex in ids) {
				if (nIndex == ids.length - 1) {
					tempObject[ids[nIndex]] = e.detail.value;
				} else {
					if (!tempObject[ids[nIndex]]) {
						tempObject[ids[nIndex]] = {};
					}
					tempObject = tempObject[ids[nIndex]];
				}
			}
		}
	}
	static hideLoading() {
		if (this.loadingNumer <= 0) return;
		this.loadingNumer--;
		if (this.loadingNumer === 0) {
			this.loadingTimer && clearTimeout(this.loadingTimer);
			uni.hideLoading();
		}
	}
	static showLoading(title = "", mask = true) {
		if (this.loadingNumer === 0) {
			uni.showLoading({
				title,
				mask,
			});

			// 最长30s自动关闭
			this.loadingTimer = setTimeout(() => {
				if (this.loadingNumer > 0) {
					uni.hideLoading();
				}
			}, 30000);
		}

		this.loadingNumer++;
	}
};
